package com.cowain.monitor.service.impl;

//import com.cowain.monitor.datasources.annotation.OperLog;
import com.cowain.monitor.datasources.annotation.OperLog;
import com.cowain.monitor.entity.*;
import com.cowain.monitor.service.*;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * PMS ehr-backend
 *
 * @Classname SynthesizeMethod
 * @Description TODO
 * @Date 2020-07-30 17:03
 * @Created 沈科举
 */
@Service
public class SynthesizeMethod {
    @Autowired
    private ErpSoService erpSoService;
    @Autowired
    private ErpSofService erpSofService;
    @Autowired
    private ErpNorDerService erpNorDerService;
    @Autowired
    private ErpClientService erpClientService;
    @Autowired
    private ErpSupplierService erpSupplierService;
    @Autowired
    private PmsCheckDemandService pmsCheckDemandService;
    @Autowired
    private ErpMoService erpMoService;
    @Autowired
    private PmsShipmentStatusService pmsShipmentStatusService;
    @Autowired
    private ErpCustomerService erpCustomerService;
    @Autowired
    private PmsSysUserService sysUserService;
    @Autowired
    private PmsSysDeptService sysDeptService;
    @Autowired
    private ErpItemInfoService erpItemInfoService;

    /**
     * 销售订单
     */

    public void orderInfo() {
        try {
            List<ErpSoEntity> erpSoList = erpSoService.findErpSoList();
            if (erpSoList != null && erpSoList.size() > 0) {
                erpSoService.insertPmsOrderInfo(erpSoList, PmsConstantUtils.ORDER_TYPE_INFO);
                erpSoService.updateErpOrder(erpSoList);
            } else {
                System.out.println("暂时没有标准销售订单");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

    }

    /**
     * 预测订单
     */
    public void erpSofApplication() {
        try {
            List<ErpSofEntity> applicationList = erpSofService.findApplicationList();

            if (applicationList != null && applicationList.size() > 0) {
                erpSofService.insertPmsOrderInfo(applicationList, PmsConstantUtils.ORDER_TYPE_APPLICATION);
//                int i = 3/0 ;
                erpSofService.updateApplicationOrder(applicationList);
            } else {
                System.out.println("暂时没有预测订单");
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 物料申请单
     */
    public void erpNoDerOrder() {
        try {
            List<ErpNorderEntity> listOrder = erpNorDerService.findListOrder();

            if (listOrder != null && listOrder.size() > 0) {
                erpNorDerService.insertInfoOrder(listOrder, PmsConstantUtils.ORDER_TYPE_ORDERDER);
                erpNorDerService.updateOrderNo(listOrder);
            } else {
                System.out.println("暂时没有物料申请单");
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 客户信息
     */
    public void erpClient() {
        try {
            List<ErpCustomerEntity> customerList = erpCustomerService.findCustomerList();
            if (customerList != null && customerList.size() > 0) {
                erpCustomerService.insertClient(customerList);
                erpCustomerService.updateCustomer(customerList);
            }else {
                System.out.println("暂未有客户信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 供应商信息
     */
    public void erpSupplier() {
        try {
            List<ErpSupplierEntity> erpSupplierList = erpSupplierService.findErpSupplierList();
            if (erpSupplierList != null && erpSupplierList.size() > 0) {
                erpSupplierService.insertSupplier(erpSupplierList);
                erpSupplierService.updateSupplier(erpSupplierList);
            } else {
                System.out.println("暂未有供应商信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }


    @OperLog(operModul = "拉取erp成品入库信息", operType = "", operDesc = "成品入库")
    public R syncErpCheckDemand() {
        List<ErpCompleteEntity> erpCmpleteEntityList = pmsCheckDemandService.findErpCmpleteEntityByIsdel();
        if (null != erpCmpleteEntityList && 0 != erpCmpleteEntityList.size()) {
            R r = pmsCheckDemandService.addPmsCheckDemand(erpCmpleteEntityList);
            int result = (int) r.get("result");
            List<ErpCompleteEntity> updataErpCompleteList = (List<ErpCompleteEntity>) r.get("updataErpCompleteList");
            if (0 == result) {
                System.out.println("1--->  成品入库没有数据需要同步");
                return R.error("1--->  成品入库没有数据需要同步");
            }
            if (0 < result) {
                result = pmsCheckDemandService.updateErpCompleteEntityIsdel(updataErpCompleteList);
                if (0 < result) {
                    System.out.println("1--->  成品入库同步成功");
                    return R.ok("成品入库同步成功");
                }
            }
            System.out.println("1--->成品入库同步失败");
            return R.error("成品入库同步失败");
        }
        System.out.println("1--->成品入库没有需要同步的信息");
        return R.error("成品入库没有需要同步的信息");

    }

    @OperLog(operModul = "生产订单模块", operType = "", operDesc = "生产订单信息同步")
    public R syncErpOrder() {
        List<ErpMoEntity> erpMoList = erpMoService.findErpMoByIsDel(PmsConstantUtils.IS_DEL.intValue());
        if (null != erpMoList && 0 != erpMoList.size()) {
            R r = erpMoService.addPmsProductionOrderEntity(erpMoList);
            int result = (int) r.get("result");
            List<ErpMoEntity> updataErpMoList = (List<ErpMoEntity>) r.get("updataErpMoList");
            if (0 == result) {
                System.out.println("2--->生产订单没有需要同步的数据");
                return R.error("生产订单没有需要同步的数据");
            }
            if (0 < result) {
                result = erpMoService.updatErpMoById(updataErpMoList);
                if (0 < result) {
                    System.out.println("生产订单同步成功");
                    syncErpOrder();
                    return R.ok("2--->生产订单同步成功");
                }
            }
            System.out.println("2--->生产订单同步失败");
            return R.error("生产订单同步失败");
        }
        System.out.println("2--->生产订单没有需要同步数据");
        return R.error("生产订单没有需要同步数据");
    }


    @OperLog(operModul = "出货信息模块", operType = "", operDesc = "出货信息和状态同步")
    public R syncErpShipData() {
        List<ErpShipEntity> erpShipEntityList = pmsShipmentStatusService.findErpShipEntityByIsdel();
        if (null != erpShipEntityList && 0 != erpShipEntityList.size()) {
            R r = pmsShipmentStatusService.addPmsShipmentStatus(erpShipEntityList);
            int result = (int) r.get("result");
            List<ErpShipEntity> updataErpShipList = (List<ErpShipEntity>) r.get("updataErpShipList");
            if (0 == result) {
                System.out.println("3--->出货消息和状态没有需要同步的数据");
                return R.error("3--->出货消息和状态没有需要同步的数据");

            }
            if (0 < result) {
                result = pmsShipmentStatusService.addPmsDeliveryNotice(updataErpShipList);
                if (0 < result) {
                    result = pmsShipmentStatusService.updateErpShipIsdel(updataErpShipList);
                    if (0 < result) {
                        System.out.println("3--->出货消息和状态同步成功");
                        return R.ok("出货消息和状态同步成功");
                    }
                }

            }
            System.out.println("3--->出货消息和状态同步失败");
            return R.error("出货消息和状态同步失败");
        }
        System.out.println("3--->出货消息和状态没有需要同步数据");
        return R.error("出货消息和状态没有需要同步数据");
    }


    public void ehrUser() {
        Date date = sysUserService.findDate();
        List<PmsSysUserEntity> user = sysUserService.findUser(date);
        if (user != null && user.size() > 0) {
            sysUserService.insertUser(user);
            sysUserService.updateUser(user);
        } else {
            System.out.println("暂未有用户数据同步");
        }
    }

    public void ehrDept() {
        Date date = sysDeptService.findDate();
        List<PmsSysDeptEntity> dept = sysDeptService.findDept(date);
        if (dept != null && dept.size() > 0) {
            sysDeptService.insertDept(dept);
            sysDeptService.updateDeptList(dept);
        } else {
            System.out.println("暂未有组织数据同步");
        }
    }


    //同步中间库数据获取料号
//    public R synchroErpItemInfo(){
//        erpItemInfoService.sysnchroPmsData();
//        return R.ok();
//    }

    //同步中间库数据获取料号
    public R synchroErpItemInfo() {
        List<ErpItemInfoEntity> itemInfoEntities = erpItemInfoService.synchroErpItemInfo();
        ArrayList<ErpItemInfoEntity> list = new ArrayList<>();
        if (null != itemInfoEntities && itemInfoEntities.size() != 0) {
            //查询pms数据库数据
            List<String> itemIds = erpItemInfoService.findPmsData();
            //判断是否有不同数据，数据不同就导入pms库中
            //存入id
            for (ErpItemInfoEntity itemInfoEntity : itemInfoEntities) {
                if (!itemIds.contains(itemInfoEntity.getId())){
                    list.add(itemInfoEntity);
                }
            }
        }
        //同步到pms数据
        return erpItemInfoService.insertItemInfo(list);
    }
}
